package com.xingren.wms.admin.wms.controller;

import com.xingren.common.data.ApiErrors;
import com.xingren.common.data.JsonResult;
import com.xingren.common.exceptions.ApiException;
import com.xingren.normandy.model.response.UserPermissionInfo;
import com.xingren.reaper.spring.sso.ReaperSsoConfig;
import com.xingren.spring.controllers.IController;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class IndexController implements IController {

    @RequestMapping(ModuleRoutes.INDEX)
    public String index() {
        return "index";
    }

    @GetMapping(value = ModuleRoutes.AJAX_USER, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public JsonResult<UserPermissionInfo> user() {
        UserPermissionInfo user = (UserPermissionInfo) getSession().getAttribute(
                ReaperSsoConfig.SESSION_ATTRIBUTE_USER_INFO);
        return JsonResult.ok(user);
    }

    @GetMapping(value = ModuleRoutes.AJAX_USER2, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public JsonResult<UserPermissionInfo> user2() {
        throw new ApiException(ApiErrors.BASE_UNAUTHORIZED, "不允许访问！");
    }
}
